import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.lang.Math;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class MainFrame extends JFrame {
	//test variables
	private JLabel countLabel;
	private JLabel totalPegsAllowedLabel;
	private JLabel boardComponentLabel;
	private JLabel output;
	private JLabel output1;
	private JLabel output2;
	private JLabel output3;
	private JLabel output4;
	private JLabel output5;
	private JLabel output6;
	private JLabel output7;
	private JLabel output8;
	private static JLabel[][] value = new JLabel[7][7];
	static void vIn(){
		for(int i = 0; i < 7; i++){
			for(int j = 0; j < 7; j++)
			value[i][j].setText(String.format("Number: %s", i*j));
		}
		
	}
	private static JLabel[]	rowLabel = new JLabel[7];
	private static JLabel[] colLabel = new JLabel[7];
	static void rcIN(){
		for(int i = 0;i<7;i++){
			rowLabel[i].setText("Row " + i);
			colLabel[i].setText("Row " + i);
		}
	}
	private static boolean[] rowvalue = new boolean[7];
	private static boolean[] colvalue = new boolean[7];
	static void rowcolval(){
		for(int i = 0;i<7;i++){
			rowvalue[i] = false;
			colvalue[i] = false;
		}
	}
	
	private int sum;
	private int sumcol;
	boolean flag = true; // white = true; black = false; 
	static boolean successPlace = false;
	
	//peg dimensions
	private final int pegSize = 40;
	
	//peg count
	private static int count = 0;
	private int totalPegsAllowed = 18;
	
	//variable to check if a peg was removed, if it was removed it has to be replaced
	private boolean pegRemoved = false;
	//holds the last peg color
	private static int oldPeg = 0;
	
	//peg movement
	private int allowedDistance = 1;
	//private variables for buttons 
	private int offsetX = 500;
	private int offsetY = 60;
	private int buttonNumber = 0;
	private int dimX = 80;
	private int dimY = 50;
	private int oldrow = 0;
	private int oldcol = 0;
	
	private static int blackNum = 0; // totoal num of the blackPeg in hand
	private static int whiteNum = 0; // totoal num of the whitePeg in hand
	private static int deadBlack = 0; // record the num of removed blackPeg
	private static int deadWhite = 0; // record the num of removed whitePeg
	private static boolean RemoveFlag = false; // decide whether remove or place of mouseClick
	private static boolean SuccessRemove = false; // get to know whether remove successfully
	public static boolean fouthRow1to3 = false; // get to know the whether the 4th row (1-3) is mirror
	public static boolean fouthRow5to7 = false; // get to know the whether the 4th row (5-7) is mirror
	public static boolean fouthcol1to3 = false; // get to know the whether the 4th col (1-3) is mirror
	public static boolean fouthcol5to7 = false; // get to know the whether the 4th col (5-7) is mirror
	
	
	//record the whether there is a morris in a row or in a colum
	private static boolean[] recordRow = new boolean[7];
	private static boolean[] recordColum = new boolean[7];
	
	// initial the recordRow and recordColum
	static void initialRecordRow() {
		for (int i = 0; i < 7; i++) {
			recordRow[i] = false;
		}
	}
	static void initialRecordCol() {
		for (int i = 0; i < 7; i++) {
			recordColum[i] = false;
		}
	}
	
	// 
	ImageIcon blackPeg = new ImageIcon("src/peg_black.png");
	ImageIcon whitePeg = new ImageIcon("src/peg_white.png");
	JLabel black = new JLabel(blackPeg);
	JLabel white = new JLabel(whitePeg);
	
	//boolean variable to keep track of board occupation
	//HAD TO CHANGE THIS TO [8][8] SINCE I COUDLNT PLACE PEGS ON LAST ROW
	static int[][] position = new int[7][7];
	static void initialPosition() {
		for (int j = 0; j < 7; j++) {
			for (int k= 0; k < 7; k++ )
				position[j][k] = 0;
		}
	}
	//button constructors
	private JButton save;
	private JButton load;
	private JButton exit;
	
	//varaible holding dimensions of JFrame
	private Insets insets;
	
	//variable that will hold the board picture
	private JLabel board;
	
	//constructor
	public MainFrame(){
		super("Nine Mens Morris");
		
		//default layout
		//no layout -- > place all elements manually
		//have to make layout scalable --> use variables
		setLayout(null);
		
		//get the dimensions of the JFrame to place elements at fixed positions
		insets = getInsets();
		
		//add the board picture to the frame
		ImageIcon img = new ImageIcon("src/image.jpg");
		int ycord = img.getIconHeight();
		int xcord = img.getIconWidth();
		
		board = new JLabel(img);
		board.setBounds(insets.right, insets.top,xcord, ycord);
		add(board);
		
		BoardHandler bHandler = new BoardHandler();
		board.addMouseListener(bHandler);
		board.addMouseMotionListener(bHandler);
		
		//add button handlers here
		ExitHandler eHandler = new ExitHandler();
		LoadHandler lHandler = new LoadHandler();
		SaveHandler sHandler = new SaveHandler();
		
		//add buttons here
		addButton(save, "save", sHandler);
		addButton(load, "load", lHandler);
		addButton(exit, "exit", eHandler);
		
//TESTING
		countLabel = new JLabel("count: " + getCount());
		totalPegsAllowedLabel = new JLabel("maxPegs: " + getTotalPegsAllowed());
		boardComponentLabel = new JLabel("component label: ");
		output = new JLabel("output:");
		output1 = new JLabel("output1: ");
		output2 = new JLabel("output2: ");
		output3 = new JLabel("output3:");
		output4 = new JLabel("output4:");
		output5 = new JLabel("output5:");
		output6 = new JLabel("output6:");
		output7 = new JLabel("output7:");
		output8 = new JLabel("output8:");
		
		for(int i = 0; i < 7; i++){
			for(int j = 0; j < 7; j++)
			value[i][j] = new JLabel(String.format("%s",i));
		}
		
		countLabel.setBounds(insets.right , insets.left + 8*getButtonOffsetY(),100,10);
		totalPegsAllowedLabel.setBounds(insets.right , insets.left + 9*getButtonOffsetY(),250,10);
		boardComponentLabel.setBounds(insets.right , insets.left + 13*getButtonOffsetY(),550,10);
		// 0: show position[][]
		output.setBounds(insets.right , insets.left + 10*getButtonOffsetY(),300,10);
		
		// 1: show status of the remove position (clicked)
		output1.setBounds(insets.right , insets.left + 11*getButtonOffsetY(),200,10);
		
		// 2: show status of the oldPeg
		output2.setBounds(insets.right + 200, insets.left + 11*getButtonOffsetY(),200,10);
		
		// 3: show replace position value (pegval)
		output3.setBounds(insets.right + 400, insets.left + 11*getButtonOffsetY(),200,10);
		
		// 4: show number of black and white
		output4.setBounds(insets.right + 600 , insets.left + 11*getButtonOffsetY(),200,10);
		output5.setBounds(insets.right + 200 , insets.left + 12*getButtonOffsetY(),200,10);
		output6.setBounds(insets.right + 400 , insets.left + 12*getButtonOffsetY(),200,10);
		output7.setBounds(insets.right + 600 , insets.left + 12*getButtonOffsetY(),200,10);
		// 8: show the peg in hand
		output8.setBounds(insets.right + 300, insets.left + 10*getButtonOffsetY(),550,10);
		
		for(int i = 0; i < 7; i++){
			for(int j = 0; j < 7; j++){
			value[i][j].setBounds(insets.right + 700 + j *20, insets.top+ i * 20 , 20, 20);
			add(value[i][j]);
			}			
		}
		add(countLabel);
		add(totalPegsAllowedLabel);
		add(boardComponentLabel);
		add(output);
		add(output1);
		add(output2);
		add(output3);
		add(output4);
		add(output5);
		add(output6);
		add(output7);
		add(output8);
		
//TESTING
		
		
	}
	
	//save and load function will be extra content and both are empty for now
	private void save(){}
	private void load(){}
	
	//buttons handlers
	private class LoadHandler implements MouseListener{
		public void mouseClicked(MouseEvent event){load();}
		public void mouseReleased(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent arg0) {}
		
	}
	private class SaveHandler implements MouseListener{
		public void mouseClicked(MouseEvent event){save();}
		public void mouseReleased(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent arg0) {}
		
	}
	private class ExitHandler implements MouseListener{
		public void mouseClicked(MouseEvent event){System.exit(0);}
		public void mouseReleased(MouseEvent event){}
		public void mousePressed(MouseEvent event){}
		public void mouseEntered(MouseEvent event){}
		public void mouseExited(MouseEvent arg0) {}
		
	}
	
	//function that will place button in the frame
	private void addButton(JButton button, String name, MouseListener listener){
		button = new JButton(name);
		button.setBounds(insets.right + getButtonOffsetX(), insets.left + getButtonNumber()*getButtonOffsetY(),
				getButtonDimX(), getButtonDimY());
		
		button.addMouseListener(listener);
		this.add(button);
		setButtonNumber(getButtonNumber() + 1);
		
	}

	private class BoardHandler implements MouseListener, MouseMotionListener{

		@Override
		public void mouseDragged(MouseEvent e) {
			//e.getLocationOnScreen();
			//RemovePeg(e);
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			
			if (RemoveFlag == true){
				RemovePeg(e);
				if (SuccessRemove == true) {
					RemoveFlag = false;
					SuccessRemove = false;
				}
			}
			//phase 1: place all pegs
			else 
				if ((flag == false && blackNum < 9)||(flag == true && whiteNum < 9)) { 
					PlacePeg(e);
					if (successPlace == true) {
						flag = !(flag);
						successPlace = false;
						MainFrame.setCount(getCount() + 1);
						countLabel.setText("count: " + getCount());
					}
				}
			//phase 2: move pegs/ remove pegs
			else 
				if((whiteNum == 9 || blackNum == 9) && pegRemoved == false) {
					pegRemoved = true;
					RemovePeg(e);
				}
			else 
				if((whiteNum == 9 || blackNum == 9) && pegRemoved) {
					pegRemoved = false;
					
					// add one back for remove action
					if (flag)
						deadWhite = deadWhite + 1;
					else
						deadBlack = deadBlack + 1;
					
					output6.setText("oldPeg: " + oldPeg);
					ReplacePeg(e, oldPeg);
					//oldPeg = 0;
					flag = !(flag);
				}	
			output8.setText("next color: " + flag);
			output4.setText("blackPlaced: " + blackNum 
					+ "whitePlaced: " + whiteNum);
			repaint();			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		
	}
	
	public void DoReplacePeg(MouseEvent event, int row, int col, JLabel peg){
		//if the position is empty place the peg
		row = row-1;
		col = col-1;
		
		if(position[row][col] == 0){
			peg.setBounds(event.getX() - (pegSize / 2), event.getY()
					- (pegSize / 2), pegSize, pegSize + 1);
			//output4.setText("OldPeg is now a " + oldPeg + " peg.");
			output5.setText("OldPeg is " + oldPeg + col + " " + row);
			position[row][col] = oldPeg;
			
			output6.setText("Position is now a " + position[row][col]);
			
			board.add(peg);
			repaint();			
		}
		else output7.setText("That was not an empty spot");
	}

	public void ReplacePeg(MouseEvent event, int pegval) {
		// board numbering from left to right starting at 1
		// places the players peg
		ImageIcon blackPeg = new ImageIcon("src/peg_black.png");
		ImageIcon whitePeg = new ImageIcon("src/peg_white.png");
		JLabel black = new JLabel(blackPeg);
		JLabel white = new JLabel(whitePeg);
		
		int row = 0;
		JLabel peg = new JLabel();
		if(pegval == 1)	peg = white;
		if(pegval == -1)peg = black;
		if(pegval == 0)	output3.setText("ERROR, ZERO VALUE!");
				
		// row 1
		if (event.getY() > 20 && event.getY() < 40) {
		row = 1;
			// position 1,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoReplacePeg(event, row, 1, peg);
			}
			// position 1,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoReplacePeg(event, row, 4, peg);
			}
			// position 1,3
			if (event.getX() > 390 && event.getX() < 410) {
				DoReplacePeg(event, row, 7, peg);
			}
		}

		// row 2
		if (event.getY() > 80 && event.getY() < 100) {
			row = 2;
			// position 2,1
			if (event.getX() > 80 && event.getX() < 100) {
				DoReplacePeg(event, row, 2, peg);
			}
			// position 2,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoReplacePeg(event, row, 4, peg);
			}
			// position 2,3
			if (event.getX() > 335 && event.getX() < 355) {
				DoReplacePeg(event, row, 6, peg);
			}
		}
				
		// row 3
		if (event.getY() > 130 && event.getY() < 150) {
			row = 3;
			// position 3,1
			if (event.getX() > 130 && event.getX() < 150) {
				DoReplacePeg(event, row, 3, peg);
			}
			// position 3,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoReplacePeg(event, row, 4, peg);
			}
			// position 3,3
			if (event.getX() > 280 && event.getX() < 300) {
				DoReplacePeg(event, row, 5, peg);
			}
			
		}

		// row 4 (6 pegs)
		if (event.getY() > 205 && event.getY() < 225) {
			row = 4;
			// position 4,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoReplacePeg(event, row, 1, peg);
			}
			// position 4,2
			if (event.getX() > 80 && event.getX() < 100) {
				DoReplacePeg(event, row, 2, peg);
			}
			// position 4,3
			if (event.getX() > 130 && event.getX() < 150) {
				DoReplacePeg(event, row, 3, peg);
			}
			// position 4,5
			if (event.getX() > 285 && event.getX() < 305) {
				DoReplacePeg(event, row, 5, peg);
			}
			// position 4,6
			if (event.getX() > 335 && event.getX() < 355) {
				DoReplacePeg(event, row, 6, peg);
			}
			// position 4,7
			if (event.getX() > 390 && event.getX() < 410) {
				DoReplacePeg(event, row, 7, peg);
			}
			
		}

		// row 5
		if (event.getY() > 290 && event.getY() < 310) {
			row = 5;
			// position 5,1
			if (event.getX() > 130 && event.getX() < 150) {
				DoReplacePeg(event, row, 3, peg);
			}
			// position 5,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoReplacePeg(event, row, 4, peg);
			}
			// position 5,3
			if (event.getX() > 280 && event.getX() < 300) {
				DoReplacePeg(event, row, 5, peg);
			}
		}

		// row 6
		if (event.getY() > 335 && event.getY() < 355) {
			row = 6;
			// position 6,1
			if (event.getX() > 80 && event.getX() < 100) {
				DoReplacePeg(event, row, 2, peg);
			}
			// position 6,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoReplacePeg(event, row, 4, peg);
			}
			// position 6,3
			if (event.getX() > 335 && event.getX() < 355) {
				DoReplacePeg(event, row, 6, peg);
			}
		}

		// row 7
		if (event.getY() > 390 && event.getY() < 410){
			row = 7;
			// position 7,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoReplacePeg(event, row, 1, peg);
			}
			// position 7,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoReplacePeg(event, row, 4, peg);
			}
			// position 7,3
			if (event.getX() > 390 && event.getX() < 410) {
				DoReplacePeg(event, row, 7, peg);
			}		
		}
		DoJudge();
		repaint();
	}

	//this method will 'pcik up' a peg from the board (remove it) and remember the color of that peg, and replace it again
	//restrictions have to be applied when flying is not active (>3 pegs) and will let the player only move allowedDistance at a time
	public void RemovePeg(MouseEvent event) {
		// board numbering from left to right starting at 1
		// places the players peg
		int row = 0;
		JLabel peg = new JLabel();
				
		// row 1
		if (event.getY() > 20 && event.getY() < 40) {
		row = 1;
			// position 1,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoRemovePeg(event, row, 1, peg);
			}
			// position 1,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoRemovePeg(event, row, 4, peg);
			}
			// position 1,3
			if (event.getX() > 390 && event.getX() < 410) {
				DoRemovePeg(event, row, 7, peg);
			}
		}

		// row 2
		if (event.getY() > 80 && event.getY() < 100) {
			row = 2;
			// position 2,1
			if (event.getX() > 80 && event.getX() < 100) {
				DoRemovePeg(event, row, 2, peg);
			}
			// position 2,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoRemovePeg(event, row, 4, peg);
			}
			// position 2,3
			if (event.getX() > 335 && event.getX() < 355) {
				DoRemovePeg(event, row, 6, peg);
			}
		}
				
		// row 3
		if (event.getY() > 130 && event.getY() < 150) {
			row = 3;
			// position 3,1
			if (event.getX() > 130 && event.getX() < 150) {
				DoRemovePeg(event, row, 3, peg);
			}
			// position 3,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoRemovePeg(event, row, 4, peg);
			}
			// position 3,3
			if (event.getX() > 280 && event.getX() < 300) {
				DoRemovePeg(event, row, 5, peg);
			}
			
		}

		// row 4 (6 pegs)
		if (event.getY() > 205 && event.getY() < 225) {
			row = 4;
			// position 4,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoRemovePeg(event, row, 1, peg);
			}
			// position 4,2
			if (event.getX() > 80 && event.getX() < 100) {
				DoRemovePeg(event, row, 2, peg);
			}
			// position 4,3
			if (event.getX() > 130 && event.getX() < 150) {
				DoRemovePeg(event, row, 3, peg);
			}
			// position 4,5
			if (event.getX() > 285 && event.getX() < 305) {
				DoRemovePeg(event, row, 5, peg);
			}
			// position 4,6
			if (event.getX() > 335 && event.getX() < 355) {
				DoRemovePeg(event, row, 6, peg);
			}
			// position 4,7
			if (event.getX() > 390 && event.getX() < 410) {
				DoRemovePeg(event, row, 7, peg);
			}
			
		}

		// row 5
		if (event.getY() > 290 && event.getY() < 310) {
			row = 5;
			// position 5,1
			if (event.getX() > 130 && event.getX() < 150) {
				DoRemovePeg(event, row, 3, peg);
			}
			// position 5,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoRemovePeg(event, row, 4, peg);
			}
			// position 5,3
			if (event.getX() > 280 && event.getX() < 300) {
				DoRemovePeg(event, row, 5, peg);
			}
		}

		// row 6
		if (event.getY() > 335 && event.getY() < 355) {
			row = 6;
			// position 6,1
			if (event.getX() > 80 && event.getX() < 100) {
				DoRemovePeg(event, row, 2, peg);
			}
			// position 6,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoRemovePeg(event, row, 4, peg);
			}
			// position 6,3
			if (event.getX() > 335 && event.getX() < 355) {
				DoRemovePeg(event, row, 6, peg);
			}
		}

		// row 7
		if (event.getY() > 390 && event.getY() < 410){
			row = 7;
			// position 7,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoRemovePeg(event, row, 1, peg);
			}
			// position 7,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoRemovePeg(event, row, 4, peg);
			}
			// position 7,3
			if (event.getX() > 390 && event.getX() < 410) {
				DoRemovePeg(event, row, 7, peg);
			}		
		}
		repaint();
	}

	public void DoRemovePeg(MouseEvent event, int row, int col, JLabel peg) {
		row = row - 1;
		col = col - 1;
		if (row == 3)
			if (fouthRow1to3 == true || fouthRow5to7 == true)
				return;
		
		if (col == 3)
			if (fouthcol1to3 ==  true || fouthcol5to7 == true)
				return;
	
		//if the clicked field is empty do nothing
		if(position[row][col] == 0 || recordRow[row] == true || recordColum[col] == true)
			output1.setText("This is an empty Field" + recordRow[row] + recordColum[col]);
		//if the clicked field has a peg (-1/1) in it take that peg and replace it at the next clicked position
		//important! we dont want to adjust the peg count, because the count tells us what phase we are in--> once we reached max pegs we are in phase 2 (moving/ play)
		else {
			if (flag == false) {
				if (deadBlack != 7 && position[row][col] == -1) {
					output1.setText("You removed a " + position[row][col] + " peg.");
					board.remove(board.getComponentAt(event.getX(),event.getY()));
					oldPeg = position[row][col];
					oldrow = row;
					oldcol = col;
					deadBlack = deadBlack + 1;
					SuccessRemove = true; 
					output2.setText("deadBlack: " + deadBlack);
					position[row][col] = 0;
					//output3.setText("OldPeg is now a " + oldPeg + " peg.");
					repaint();
				}
				else
					if (deadBlack == 7) {
						System.out.println("White Win!!!");
						System.exit(0);
					}
			}
			else
				if (deadWhite != 7 && position[row][col] == 1) {
					output1.setText("You removed a " + position[row][col] + " peg.");
					board.remove(board.getComponentAt(event.getX(),event.getY()));
					oldPeg = position[row][col];
					oldrow = row;
					oldcol = col;
					SuccessRemove = true; 
					deadWhite = deadWhite + 1;
					//output2.setText("OldPeg is now a " + oldPeg + " peg.");
					position[row][col] = 0;
					output3.setText("deadWhite: " + deadWhite);
					repaint();
				}
				else
					if (deadWhite == 7) {
						System.out.println("Black Win!!!");
						System.exit(0);
					}		
		}
	}

	
	//peg placement
	@SuppressWarnings("deprecation")
	public void DoPlacePeg (MouseEvent event, int row, int col, JLabel peg) {
		//initial peg placement
		
		row = row - 1;
		col = col - 1;
		if (position[row][col] == 0) {
			peg.setBounds(event.getX() - (pegSize / 2), event.getY()
					- (pegSize / 2), pegSize, pegSize + 1);
		
			if (flag == false) { // black
				if (blackNum != 9) {
					position[row][col] = -1;
					blackNum = blackNum + 1;
					//TODO testing
					output.setText(String.format("position %s", position[row][col]));
					board.add(peg);
					successPlace = true;
				}
			}
			else {	// white
				if (whiteNum != 9) {
					position[row][col] = 1;
					whiteNum = whiteNum + 1;
					//TODO testing
					output.setText(String.format("position %s", position[row][col]));
					board.add(peg);
					successPlace = true;
				}
			}
		}
		//TODO testing--> remove before final
		boardComponentLabel.setText("component: " + board.getComponentAt(event.getX(),event.getY()));
	}
	
	public void PlacePeg(MouseEvent event) {
		// board numbering from left to right starting at 1
		// places the players peg
		ImageIcon blackPeg = new ImageIcon("src/peg_black.png");
		ImageIcon whitePeg = new ImageIcon("src/peg_white.png");
		JLabel black = new JLabel(blackPeg);
		JLabel white = new JLabel(whitePeg);
		
		int row = 0;
		JLabel peg = new JLabel();
		peg = white; 
		if (flag == false)
			peg = black;
		
		// row 1
		if (event.getY() > 20 && event.getY() < 40){
			row = 1;
			// position 1,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoPlacePeg(event, row, 1, peg);
			}
			// position 1,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoPlacePeg(event, row, 4, peg);
			}
			// position 1,3
			if (event.getX() > 390 && event.getX() < 410) {
				DoPlacePeg(event, row, 7, peg);
			}
	
		}

		// row 2
		if (event.getY() > 80 && event.getY() < 100) {
			row = 2;
			// position 2,1
			if (event.getX() > 80 && event.getX() < 100) {
				DoPlacePeg(event, row, 2, peg);
			}
			// position 2,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoPlacePeg(event, row, 4, peg);
			}
			// position 2,3
			if (event.getX() > 335 && event.getX() < 355) {
				DoPlacePeg(event, row, 6, peg);
			}
		}
		
		// row 3
		if (event.getY() > 130 && event.getY() < 150) {
			row = 3;
			// position 3,1
			if (event.getX() > 130 && event.getX() < 150) {
				DoPlacePeg(event, row, 3, peg);
			}
			// position 3,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoPlacePeg(event, row, 4, peg);
			}
			// position 3,3
			if (event.getX() > 280 && event.getX() < 300) {
				DoPlacePeg(event, row, 5, peg);
			}
	
		}

		// row 4 (6 pegs)
		if (event.getY() > 205 && event.getY() < 225) {
			row = 4;
			// position 4,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoPlacePeg(event, row, 1, peg);
			}
			// position 4,2
			if (event.getX() > 80 && event.getX() < 100) {
				DoPlacePeg(event, row, 2, peg);
			}
			// position 4,3
			if (event.getX() > 130 && event.getX() < 150) {
				DoPlacePeg(event, row, 3, peg);
			}
			// position 4,5
			if (event.getX() > 285 && event.getX() < 305) {
				DoPlacePeg(event, row, 5, peg);
			}
			// position 4,6
			if (event.getX() > 335 && event.getX() < 355) {
				DoPlacePeg(event, row, 6, peg);
			}
			// position 4,7
			if (event.getX() > 390 && event.getX() < 410) {
				DoPlacePeg(event, row, 7, peg);
			}
	
		}

		// row 5
		if (event.getY() > 290 && event.getY() < 310) {
			row = 5;
			// position 5,1
			if (event.getX() > 130 && event.getX() < 150) {
				DoPlacePeg(event, row, 3, peg);
			}
			// position 5,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoPlacePeg(event, row, 4, peg);
			}
			// position 5,3
			if (event.getX() > 280 && event.getX() < 300) {
				DoPlacePeg(event, row, 5, peg);
			}
	
		}

		// row 6
		if (event.getY() > 335 && event.getY() < 355) {
			row = 6;
			// position 6,1
			if (event.getX() > 80 && event.getX() < 100) {
				DoPlacePeg(event, row, 2, peg);
			}
			// position 6,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoPlacePeg(event, row, 4, peg);
			}
			// position 6,3
			if (event.getX() > 335 && event.getX() < 355) {
				DoPlacePeg(event, row, 6, peg);
			}

		}

		// row 7
		if (event.getY() > 390 && event.getY() < 410){
			row = 7;
			// position 7,1
			if (event.getX() > 20 && event.getX() < 40) {
				DoPlacePeg(event, row, 1, peg);
			}
			// position 7,2
			if (event.getX() > 210 && event.getX() < 230) {
				DoPlacePeg(event, row, 4, peg);
			}
			// position 7,3
			if (event.getX() > 390 && event.getX() < 410) {
				DoPlacePeg(event, row, 7, peg);
			}
		}
		
		//TODO testing
		for(int i = 0; i < 7; i++){
			for(int j = 0; j < 7; j++)
				value[i][j].setText(String.format("%d",position[i][j]));
			
		}
		DoJudge();
		repaint();
	}
	
	// use to Judge whether there is a mill
	public void DoJudge() {
		
		for(int i = 0; i < 7; i++){	
			// get rid of i = 3, which is judged in the JudgeMorrisRow and JudgeMorrisColumn
			if(i == 3) {
				JudgeMorrisRow(i);
				JudgeMorrisColumn(i);
				continue;
			}
			if(JudgeMorrisRow(i)==true){
				//output1.setText("its 3 in a row, sum " + sum + " i = " + i + " row: "+ recordRow[i] + " col: " + recordColum[i]);
				if (recordRow[i] != true){
					recordRow[i] = true;	
					//TODO testing
					//output3.setText("NEW MORRIS, you can remove / ROW");
					output8.setText(String.format("nothing new at row %d", i));
					RemoveFlag = true;
				}
			}
			if(JudgeMorrisColumn(i)==true){
				output2.setText("its 3 in a colum, sum " + sumcol + " i = " + i + " row: "+recordRow[i] + " col: " + recordColum[i]);
				if (recordColum[i] != true) {
					recordColum[i] = true;
					//TODO testing
					//output4.setText("NEW MORRIS, you can remove / COL");
					RemoveFlag = true;
				}
			}
		}
	}
	// judge morris
	public boolean JudgeMorrisRow(int row) {
		sum = 0;
		if (row == 3) {
			for (int k = 0; k < 3; k++) {
				sum = sum + position[row][k];
			}
			if (sum == 3 || sum == -3) {
				// judge for the fourth row (1-3), whether it is new mill
				if (fouthRow1to3 == false) {
					fouthRow1to3 = true;
					RemoveFlag = true;
				}
			}
			
			sum = 0;
			for (int k = 4; k < 7; k++) {
				sum = sum + position[row][k];
			}
			if (sum == 3 || sum == -3) {
				// judge for the fourth row (5-7), whether it is new mill
				if (fouthRow5to7 == false) {
					fouthRow5to7 = true;
					RemoveFlag = true;
				}
			}
		}
		else {
			sum = 0;
			for (int k = 0; k < 7; k++) {
				sum = sum + position[row][k];
			}
			if (sum == 3 || sum == -3) {
				return true;
			}
		}
		
		return false;
	}

	public boolean JudgeMorrisColumn(int col) {
		sum = 0;
		if (col == 3) {			
			for (int k = 0; k < 3; k++) {
				sum = sum + position[k][col];
			}
			if (sum == 3 || sum == -3) {
				// judge for the fourth col (1-3), whether it is new mill
				if (fouthcol1to3 == false) {
					fouthcol1to3 = true;
					RemoveFlag = true;
				}
			}
			
			sum = 0;
			for (int k = 4; k < 7; k++) {
				sum = sum + position[k][col];
			}
			if (sum == 3 || sum == -3) {
				// judge for the fourth col (5-7), whether it is new mill
				if (fouthcol5to7 == false) {
					fouthcol5to7 = true;
					RemoveFlag = true;
				}
			}
		}
		else {
			sum = 0;
			for (int k = 0; k < 7; k++) {
				sum = sum + position[k][col];
			}
			if (sum == 3 || sum == -3) {
				return true;
			}
		}
		return false;
	}	
	
	//get and set functions for buttons
	private int getButtonOffsetX(){return offsetX;}
	private int getButtonOffsetY(){return offsetY;}
	private int getButtonNumber(){return buttonNumber;}
	private int getButtonDimX(){return dimX;}
	private int getButtonDimY(){return dimY;}
	private int getTotalPegsAllowed(){return totalPegsAllowed;}
	
	public static int getCount(){return count;}
	public static void setCount(int count){MainFrame.count = count;}
	private void setButtonNumber(int number){buttonNumber = number;}
	public void setTotalPegsAllowed(int number){totalPegsAllowed = number;}
}


